import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../utils/ScreenAdapter.dart';
import '../utils/utils.dart';

const Color _backgroundColor = Color.fromRGBO(0, 0, 0, 0.2);
const Color _borderColor = Color.fromRGBO(0, 0, 0, 0);

class BfNavBar extends StatefulWidget {
  BfNavBar({
    Key key,
    this.backgroundDecoration,
    this.backgroundColor = _backgroundColor,
    this.stateDecoration,
    this.stateBarColor,
    this.isShowLeading = true,
    this.leading,
    this.center,
    this.actions,
    this.leadingPadding,
    this.actionPadding,
    this.titleColor = Colors.white,
    this.title = '',
    this.borderHeight = 0,
    this.borderColor = _borderColor,
  }) : super(key: key);

  // 背景颜色
  final Color backgroundColor;
  // 背景装饰(一般是渐变色)
  final backgroundDecoration;
  // 状态栏背景装饰(一般是渐变色)
  final Color stateBarColor;
  // 状态栏背景颜色
  final stateDecoration;
  // 是否显示左侧插槽
  final bool isShowLeading;
  // 状态栏左侧插槽
  final Widget leading;
  // 状态栏中央插槽
  final Widget center;
  // 中央插槽文字颜色
  final Color titleColor;
  // 中央插槽文字
  final String title;
  // 状态栏右侧插槽
  final Widget actions;
  // 右侧插槽padding
  final double actionPadding;
  // 左侧插槽padding
  final double leadingPadding;
  // 底部边框高度
  final double borderHeight;
  // 底部边框颜色
  final Color borderColor;
  // 获取状态栏高度
  double _topBloodHeight = Utils.systemInfo["topBloodHeight"];

  @override
  _BfNavBarState createState() => _BfNavBarState();
}

class _BfNavBarState extends State<BfNavBar> {
  @override
  Widget build(BuildContext context) {
    //左侧组件集合
    Widget _leading = widget.leading;
    double _leadingPadding = widget.leadingPadding;
    if (null == _leadingPadding) {
      _leadingPadding = ScreenAdapter.width(22);
    }

    /// MARK:导航栏按钮
    Widget _getNavBack() {
      return InkWell(
          onTap: Utils.pageBack,
          child: Container(
            child: Icon(
              IconData(0xe64f, fontFamily: "iconfont"),
              color: widget.titleColor,
              size: 18,
            ),
          ));
    }

    //默认左侧返回按钮
    if (widget.isShowLeading && null == _leading) {
      _leading = _getNavBack();
    }

    //中间组件
    Widget _center = widget.center;
    if (null == _center) {
      _center = Container(
        alignment: Alignment.center,
        child: Text(
          widget.title,
          style: TextStyle(
            color: widget.titleColor,
            fontSize: 18,
          ),
        ),
      );
    }

    // 右侧组件
    Widget _actions = widget.actions;
    double _actionPadding = widget.actionPadding;
    if (null == _actionPadding) {
      _actionPadding = 10;
    }
    // 返回组件
    return Column(
      children: <Widget>[
        // statuBar
        Container(
          height: (widget.stateBarColor == null) ? 0 : widget._topBloodHeight,
          decoration: widget.stateDecoration == null
              ? BoxDecoration(
                  color: widget.stateBarColor ?? widget.backgroundColor,
                )
              : widget.stateDecoration,
        ),
        // navBar
        Container(
          padding: EdgeInsets.only(
            top: (widget.stateBarColor == null) ? widget._topBloodHeight : 0,
          ),
          width: ScreenAdapter.getScreenWidth(),
          height: (widget.stateBarColor == null) ? (50.0 + widget._topBloodHeight) : 50.0,
          decoration: widget.backgroundDecoration == null
              ? BoxDecoration(
                  color: widget.backgroundColor,
                )
              : widget.backgroundDecoration,
          child: Stack(
            children: [
              Container(
                height: 50,
                padding: EdgeInsets.only(left: _leadingPadding),
                alignment: Alignment(-1, 0),
                child: _leading,
              ),
              Container(
                alignment: Alignment.center,
                child: _center,
              ),
              Container(
                height: 50,
                alignment: Alignment(1, 0),
                padding: EdgeInsets.only(right: _actionPadding),
                child: _actions,
              )
            ],
          ),
        ),
        Divider(
          height: 0,
          thickness: widget.borderHeight,
          color: widget.borderColor,
        )
      ],
    );
  }
}
